package com.supermarket.common;

import lombok.Data;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 统一响应结果类
 * @param <T>
 */

@Data
public class ResponseResult<T> {
    private int code; // 状态码
    private String message; // 消息
    private T data; // 数据

    // 私有构造方法，防止外部直接实例化
    private ResponseResult(int code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    // 成功响应
    @ResponseBody
    public static <T> ResponseResult<T> success(T data) {
        ResponseResult<T> r = new ResponseResult<>(200, "成功", data);
        System.out.println(r);
        return r;
    }

    // 错误响应
    @ResponseBody
    public static <T> ResponseResult<T> error(int code, String message) {
        ResponseResult<T> r = new ResponseResult<>(code, message, null);
        System.out.println(r);
        return r;
    }
}